package com.csxy.dyq.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.csxy.dyq.exception.MyException;
import com.csxy.dyq.service.VodService;
import com.csxy.dyq.util.ConstantVodUtils;
import com.csxy.dyq.util.InitObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadAliyunVideo(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        //title: 上传之后显示名称
        String title = fileName.substring(0,fileName.lastIndexOf("."));
        //inputStream: 上传文件的输入流
        try {
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESSKEY_ID
                    , ConstantVodUtils.ACCESSKEY_SECRET
                    , title, fileName
                    , inputStream);

            UploadVideoImpl uploadVideo = new UploadVideoImpl();
            UploadStreamResponse response = uploadVideo.uploadStream(request);
            System.out.println("RequestId = " + response.getRequestId() + "\n"); //请求视频点播服务的请求ID
            String videoId = null;
            if (response.isSuccess()) {
                videoId = response.getVideoId();
                System.out.println("VideoId = " + getPlayUrl(videoId));
            }else {//如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。
                videoId = response.getVideoId();
                System.out.println("VideoId = " + getPlayUrl(videoId));
            }
            return videoId;

        } catch (IOException  e) {
            throw new MyException(20001,"文件上传失败");
        }
    }
    //根据ID删除
    @Override
    public void removeAliyunVideoById(String id) {
        try {
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESSKEY_ID,
                    ConstantVodUtils.ACCESSKEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(id);
            DeleteVideoResponse response = client.getAcsResponse(request);
        }catch (ClientException  e) {
            throw new MyException(20001,"视频删除失败");
        }
    }
    //批量删除
    @Override
    public void removeMoreVideo(List<String> videoIdList) {
        String str = StringUtils.join(videoIdList.toArray(),",");
        try {
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESSKEY_ID,
                    ConstantVodUtils.ACCESSKEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(str);
            DeleteVideoResponse response = client.getAcsResponse(request);
        }catch (Exception  e) {
            throw new MyException(20001,"视频删除失败");
        }
    }

    public String getPlayUrl(String videoId) {
        try {
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESSKEY_ID,
                    ConstantVodUtils.ACCESSKEY_SECRET);
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            request.setVideoId(videoId);
            GetPlayInfoResponse response = client.getAcsResponse(request);
            return response.getPlayInfoList().get(0).getPlayURL();
        }catch (ClientException  e) {
            throw new MyException(20001,"获取播放地址失败");
        }
    }
}
